<?php
/*
 usersOnline.class.php
 Author: Ilir Fekaj
 Contact: tebrino@hotmail.com
 Last updated: July 28, 2005
 Version: 1.1
 Latest version & info: http://www.sim-php.info
 Support: http://forum.sim-php.info/ (if you find bugs or you need help with installation)
 Demo: http://www.free-midi.org

 This very simple class enables you to track number of visitors online in
 an easy and accurate manner. It's free for all purposes, just please don't
 claim you wrote it. If you have any problems, please feel free to contact me.
 Also if you like this script please put link to http://www.sim-php.info. Thanks

 Simply paste this code where you wish your users online count to appear:

 include_once ("usersOnline.class.php");
 $visitors_online = new usersOnline();

 if (count($visitors_online->error) == 0) {

 if ($visitors_online->count_users() == 1) {
 echo "There is " . $visitors_online->count_users() . " visitor online";
 }
 else {
 echo "There are " . $visitors_online->count_users() . " visitors online";
 }
 }
 else {
 echo "<b>Users online class errors:</b><br /><ul>\r\n";
 for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
 echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
 }
 echo "</ul>\r\n";

 }


 Important: You need to create database connection and select database before creating object! Example connection would look like this:

 $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost
 $user = ""; // your MySQL username
 $pass = ""; // your MySQL password
 $db = ""; // the database to which you're trying to connect to

 $conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
 mysql_select_db("$db", $conn);

 --------------------------------------------
 Table structure (paste this code in PHPMyAdmin or whatever program you use for db management):
 CREATE TABLE `useronline` (
 `id` int(10) NOT NULL auto_increment,
 `ip` varchar(15) NOT NULL default '',
 `timestamp` varchar(15) NOT NULL default '',
 PRIMARY KEY (`id`),
 UNIQUE KEY `id`(`id`)
 ) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;

 */

class Defined_UsersOnline {

	function Defined_UsersOnline () {
	}

	/*
	 This function will try to find out if user is coming behind proxy server. Why is this important?
	 If you have high traffic web site, it might happen that you receive lot of traffic
	 from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
	 This function tryes to get real IP address.
	 Note that getenv() function doesn't work when PHP is running as ISAPI module
	 */
	function statistics()
	{
		$db = Zend_Registry::get('db');		
		$config = Zend_Registry::get('config_site');
		// Have they visited before?
		/*
		$row = $db->fetchRow("SELECT id FROM useronline WHERE ip = ?", array ($_SERVER['REMOTE_ADDR']));
		if(!$row){
			// They have visited before
			// Update last active time
			$db->query("UPDATE useronline SET timestamp = ? WHERE ip = ? ", array(time(), $_SERVER['REMOTE_ADDR']));
		} else {
			// They have not visited before
			// Insert new row
			$db->insert('useronline', array('ip'=>$_SERVER['REMOTE_ADDR'], 'timestamp'=>time()));
		}
		*/
		
		if (!isset($_SESSION['VISITING'])) {
			//check current day
			$check = $db->fetchRow("SELECT count FROM counter WHERE time = ?",date('Y-m-d'));
			if($check) {
				$db->query("UPDATE counter SET count=count + 1 WHERE time = ?",date('Y-m-d'));
			} else {
				$db->insert('counter', array('count'=>1, 'time'=>date('Y-m-d')));
			}
			$_SESSION['VISITING'] = 1;
		}

		// Delete any old records (where lastactive < NOW - 5 Mins)
		//$db->delete('useronline',"timestamp < " . (time() - 300));

		//$qToday = mysql_query("SELECT count FROM counter where time='".date('Y-m-d')."'");
		//$today = mysql_fetch_array($qToday);

		// Get the row count
		$yesterday = $db->fetchRow("SELECT count FROM counter WHERE time=?",date('Y-m-d',strtotime("-1 day")));
		if ($yesterday['count'] == NULL) $yesterday['count'] = 0;
		$statistics['yesterday'] = $yesterday['count'] + $config['statistics']['yesterday'];

		$previousMonth = date("Y-m-d", strtotime(date('Y') . "-".date('m') . "-01 -1 months"));
		$currentMonth = date("Y-m-d", strtotime(date('Y') . "-".date('m') . "-01"));
		$lastMonth = $db->fetchRow("SELECT SUM(count) AS total FROM counter WHERE time BETWEEN ? AND ?", array($previousMonth,$currentMonth));
		if ($lastMonth['total'] == NULL)  
			$lastMonth['total'] = 0;		
		$statistics['lastmonth'] = $lastMonth['total'] + $config['statistics']['lastmonth'];

		//$onlineCount['online'] = $oCount;
		//$onlineCount['today'] = $today[0];

		$newYesterday = $db->fetchRow("SELECT COUNT(id) AS total FROM motor WHERE created_time LIKE '".date('Y-m-d',strtotime('-1 day'))."%'");
		if($newYesterday['total'] == NULL) 
			$newYesterday['total'] = 0;
		$statistics['news-yesterday'] = $newYesterday['total'] + $config['statistics']['post-yesterday'];
	//	var_dump(session_id());
		return $statistics;
	}
}

?>